package com.example.index.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.index.mapper.CollectMapper;
import com.example.index.service.impl.FolderServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yc.biz.BizException;
import com.example.index.mapper.FolderMapper;
import com.yc.pojo.Collect;
import com.yc.pojo.CollectDTO;
import com.yc.pojo.Folder;
import com.yc.pojo.House;
import com.yc.vo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("folder")
public class FolderController {

    @Resource
    FolderMapper folderMapper;

    @Resource
    CollectMapper collectMapper;

    @Resource
    FolderServiceImpl folderService;

    @Resource
    HouseController houseController;

    @RequestMapping("selectCollectByFolderId")
    public Map selectCollectByFolderId(Integer folderId,
                                       @RequestParam(defaultValue = "1") Integer current){
        MPJLambdaWrapper mpjLambdaQueryWrapper = new MPJLambdaWrapper<CollectDTO>()
                .selectAll(Folder.class)
                .selectAll(Collect.class)
                .innerJoin(Folder.class, Folder::getId, Collect::getFolderId)
                .selectAll(House.class)
                .innerJoin(House.class, House::getId, Collect::getHouseId)
//                .selectAll(Category.class)
//                .innerJoin(Category.class, Category::getId, Article::getCategoryId)
                .eq(Collect::getFolderId, folderId);
        Page<CollectDTO> p = new PageDTO<>(current,3);
        final Page page = collectMapper.selectJoinPage(p, CollectDTO.class, mpjLambdaQueryWrapper);
        final List<CollectDTO> list = page.getRecords();
        for (CollectDTO collect : list) {
            final Integer houseId = collect.getHouseId();
            final String image = houseController.image(houseId);
            collect.setImage(image);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("collects",list);
        map.put("total",page.getTotal());
        return map;
    }

    @RequestMapping("selectFolder")
    public Map selectFolder(@RequestParam(defaultValue = "1") Integer current, Integer userId, Integer folderId){
        Map<String, Object> map = new HashMap<>();
        List<Folder> folderList = new ArrayList<>();
        if (userId != null){
            LambdaQueryWrapper<Folder> folderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (folderId == null){
                folderLambdaQueryWrapper.eq(Folder::getUserId, userId);
            } else {
                folderLambdaQueryWrapper.eq(Folder::getUserId, userId).ne(Folder::getId, folderId);
            }
            Page<Folder> p = new PageDTO<>(current,3);
            final Page page = folderMapper.selectPage(p, folderLambdaQueryWrapper);
            List<Integer> folderIds = new ArrayList<>();
            for (Folder folder : folderMapper.selectList(folderLambdaQueryWrapper)) {
                folderIds.add(folder.getId());
            }
            folderList = page.getRecords();
            map.put("total", page.getTotal());
            map.put("folderIds", folderIds);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Folder folder : folderList) {
            Map<String, Object> m = new HashMap<>();
            m.put("folderName", folder.getFolderName());
            m.put("folderId", folder.getId());
            m.put("collectsMap", selectCollectByFolderId(folder.getId(),1));
            mapList.add(m);
        }
        map.put("list", mapList);
        return map;
    }

    @RequestMapping("addFolder")
    public Result addFolder(String folderName, Integer userId){
        try {
            if (userId != null){
                folderService.addFolder(folderName, userId);
                return Result.success("收藏夹”"+folderName+"“添加成功!");
            }
            return Result.failure("请先前往登录!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("deleteFolder")
    public Result deleteFolder(String folderIds){
        try {
            folderService.deleteFolder(folderIds);
            return Result.success("删除成功!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("deleteFolderById")
    public Result deleteFolderById(Integer folderId){
        try {
            folderService.deleteFolder(folderId);
            return Result.success("删除成功!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("modFolderName")
    public Result modFolderName(Integer folderId, String folderName){
        try {
            folderService.modFolderName(folderId, folderName);
            return Result.success("修改成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("selectCollectNotFolder")
    public Map selectCollectNotFolder(@RequestParam(defaultValue = "1") Integer current, Integer userId){
        Map<String, Object> map = new HashMap<>();
        List<CollectDTO> list = null;
        if (userId != null){
            MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<CollectDTO>()
                    .selectAll(Collect.class)
                    .selectAll(House.class)
                    .innerJoin(House.class, House::getId, Collect::getHouseId)
                    .eq(Collect::getFolderId, 0)
                    .eq(Collect::getUserId, userId);
            final Page page = collectMapper.selectJoinPage(new Page<>(current, 3), CollectDTO.class, mpjLambdaWrapper);
            list = page.getRecords();
            for (CollectDTO collect : list) {
                final Integer houseId = collect.getHouseId();
                final String image = houseController.image(houseId);
                collect.setImage(image);
            }
            map.put("list",list);
            map.put("total", page.getTotal());
        }
        return map;
    }

    @RequestMapping("updateArticleFolder")
    public Result updateArticleByFolder(String articleIds, Integer folderId, Integer userId){
        System.out.println("articleIds = " + articleIds);
        try {
            folderService.updateArticleByFolder(articleIds, folderId, userId);
            return folderId == 0 ? Result.success("删除成功!") : Result.success("添加成功!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

}
